package com.meida.student.app.scholartyrant.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.module.bus.provider.service.BusOrderAnswerService;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.module.pay.client.entity.PayBalance;
import com.meida.module.pay.client.entity.PayCashout;
import com.meida.module.pay.client.enums.PayCashoutStatusEnum;
import com.meida.module.pay.client.enums.PayCashoutTypeEnum;
import com.meida.module.pay.provider.service.PayBalanceLogService;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.pay.provider.service.PayCashoutService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 学霸提现申请记录列表
 */
@Component
public class UserPayCashOutPageListHandler implements PageInterceptor<PayCashout> {

    @Autowired
    private PayBalanceService payBalanceService;

    @Autowired
    private PayBalanceLogService payBalanceLogService;

    @Autowired
    private PayCashoutService payCashoutService;

    @Autowired
    private BusOrderService busOrderService;

    @Autowired
    private BusOrderAnswerService busOrderAnswerService;

    @Override
    public void prepare(CriteriaQuery<PayCashout> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        Long userId = OpenHelper.getUserId();
        cq.select(PayCashout.class, "cashoutId", "money", "commission", "realMoney", "cashoutType", "status", "createTime");
        cq.eq(PayCashout.class, "userId", userId);
        cq.createJoin(AppUser.class);
        cq.orderByDesc("cashout.createTime");
    }

    @Override
    public void complete(CriteriaQuery<PayCashout> cq, List<EntityMap> result, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        PayBalance payBalance = payBalanceService.getByUserId(userId);
        if (FlymeUtils.isNotEmpty(payBalance)) {
            BigDecimal balanceAmount = payBalance.getAmount();
            BigDecimal bindAmount = payBalance.getBindAmount();
            //总资产(累计订单佣金已完成的)
            BigDecimal totalCommissionAmount = busOrderService.sumUserCommission(userId);
            extra.put("totalCommissionAmount", totalCommissionAmount);
            //可用提现余额(佣金余额)、
            extra.put("balanceAmount", bindAmount);
            //已提现金额（已经提现到账的金额）
            BigDecimal cashoutSuccessAmount = payCashoutService.sumCashoutStatus(userId, PayCashoutStatusEnum.success.getValue());
            extra.put("cashoutSuccessAmount", cashoutSuccessAmount);
            //待提现余额（提现审核中的金额）
            extra.put("cashoutDealWithAmount", balanceAmount);
            BigDecimal haveNoSettleAmount = busOrderAnswerService.sumOrderHaveNoSettleAmount(userId);
            //待结算佣金
            extra.put("haveNoSettleAmount", haveNoSettleAmount);
            result.forEach(entityMap -> {
                Integer status = entityMap.getInt("status");
                entityMap.put("statusText", "");
                PayCashoutStatusEnum payCashoutStatusEnum = PayCashoutStatusEnum.getByValue(status);
                if (FlymeUtils.isNotEmpty(payCashoutStatusEnum)) {
                    String label = payCashoutStatusEnum.getLabel();
                    entityMap.put("statusText", label);
                }
                String cashoutType = entityMap.get("cashoutType");
                PayCashoutTypeEnum payCashoutTypeEnum = PayCashoutTypeEnum.getByValue(cashoutType);
                if (FlymeUtils.isNotEmpty(payCashoutTypeEnum)) {
                    String label = payCashoutTypeEnum.getLabel();
                    entityMap.put("cashoutTypeText", label);
                }
            });
        }

    }
}
